#!/bin/env perl

BEGIN {
  use File::Path qw( remove_tree );
  use Test::More;
  
  my $sock_root = "$ENV{PWD}/root/SOCK";
  
  unshift @INC,    "$sock_root/lib";
  $ENV{PERL5LIB} = "$sock_root/lib";
  
  my $create_link = sub {
    my ( $directory, $name ) = @_;
    
    remove_tree ( $directory );
    mkdir $directory, oct 755 unless -d $directory;
    chdir 'root';
    unlink "$name,old" if -l "$name,old";
    rename $name, "$name,old";
    symlink $directory => $name;
    chdir '..';
    
    return $directory;
  };
  
  $create_link->( $ENV{ISOCK_TEST_TMP} || '/tmp/isock_test_tmp' => 'tmp' );
  $create_link->( $ENV{ISOCK_TEST_DB}  || '/tmp/isock_test_db'  => 'db'  );
  
  $SIG{INT} = sub { cleanup () };
  
  delete $ENV{$_} for qw( http_proxy ftp_proxy );
  
  $ENV{ISOCK_TEST_APP_DSN} = 'dbi:Pg:dbname=app-test',
}

END {
  cleanup ();
}

sub cleanup {
  chdir 'root';
  
  for ( qw( tmp db ) ) {
    unlink;
    rename "$_,old", $_;
  }
}

1;
